{% assign pinterest_id = storeConfig.pinterest_list | strtoarray %}
{% get_pixels_domain list={pinterest_id} %}
{%- assign pixels = list -%}
{% if pixels %}
<script>
!function(e){if(!window.pintrk){window.pintrk=function(){window.pintrk.queue.push(Array.prototype.slice.call(arguments))};var n=window.pintrk;n.queue=[],n.version="3.0";var t=document.createElement("script");t.async=!0,t.src=e;var r=document.getElementsByTagName("script")[0];r.parentNode.insertBefore(t,r)}}("https://s.pinimg.com/ct/core.js");
</script>
<script>

    (function(){
        let email =  "{{pixel_user.email | string_sha256 }}";
        const pixels = {{pixels|json}};
        function triggerPixels(name, data) {
          pixels.forEach(function(pixel) {
            if(email){
                pintrk('load', pixel.facebook_pixel_id,{em: email});
            }else{
                pintrk('load', pixel.facebook_pixel_id);
            }
            pintrk("track",name,data)
          });
        }
        
        triggerPixels('page');
        window['TOP_GLOBAL_CALLBACK_LAYER'].register({
            'view_category': function(){
                triggerPixels('ViewCategory');
            },
            'view_content': function(product){
                triggerPixels('pagevisit', {
                    currency: _GET_C_SETTING_('base_currency_code'),
                    value: product.variant.price,
                    order_quantity: 1,
                    line_items: [{
                        product_id: product.spu,
                        product_quantity: 1,
                        product_category:product.product_type.replace(/\s+$/g, ""),
                        product_price: product.variant.price,
                        product_variant_id: product.variant.sku,
                        product_variant: product.variant.sku_value.replace(/\<br \/\>/g, ","),
                    }]
                });
            },
            'search':function(value){
               triggerPixels('search', {search_query: value});
            },
            'generate_lead': function(){
                triggerPixels('lead');
            },
            'sign_up': function(){
                triggerPixels('signup');
            },
            'login': function(){
                triggerPixels('login');
            },
            'begin_checkout': function(){
                triggerPixels('begincheckout');
            },
            'purchase':function(order){
                triggerPixels('checkout', {
                    order_quantity: order.total_num,
                    {% if storeConfig.pinterest_sales_goods_amount %}
                    value: Number(order.current_subtotal_price).plus(Number(order.current_coupon_price)).plus(order.current_promotion_price),
                    {% else %}
                    value: order.total_price,
                    {% endif %}
                    currency: order.currency_code,
                    order_id: order.order_number,
                    event_id: order.order_number,
                    line_items:order.products.map(item => {
                        return {
                            product_id: item.spu,
                            product_quantity: item.quantity,
                            product_category: item.product_type.replace(/\s+$/g, ""),
                            {% if storeConfig.pinterest_sales_goods_amount %}
                            product_price: item.discount_price,
                            {% else %}
                            product_price: item.price,
                            {% endif %}
                            product_variant_id: item.sku,
                            product_variant: item.sku_value.replace(/\<br \/\>/g, ","),
                        }
                    })
                });
            },
            'add_to_cart': function(ecdata){
                var spu = [];
                spu.push(ecdata['ec_data_spu']);
                triggerPixels('addtocart', {
                    currency: _GET_C_SETTING_('base_currency_code'),
                    value: ecdata['ec_data_price']*ecdata['ec_data_quantity'],
                    order_quantity: ecdata['ec_data_quantity'],
                    line_items: [{
                        product_id: ecdata['ec_data_spu'],
                        product_quantity: ecdata['ec_data_quantity'],
                        product_category: ecdata['ec_data_category'],
                        product_price: ecdata['ec_data_price'],
                        product_variant_id: ecdata['ec_data_sku'],
                        product_variant: ecdata['ec_data_sku_name'],

                    }]
                });
            }
        });
    })()
</script>
{% endif %}
